﻿using BCCommon;
using BCData.Common.SystemExceptionRecord;
using BCDto.Common.SystemExceptionRecord;
using BCEntity.Common.SystemExceptionRecord;
using BCService.Utils;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;

namespace BCService.Common.ExceptionService
{
    public class SystemExceptionRecordService : ISystemExceptionRecordService
    {
        private readonly ISystemExceptionRecordData systemExceptionRecordData;
        //private readonly IAppExceptionRecordData appExceptionRecordData;

        public SystemExceptionRecordService(ISystemExceptionRecordData systemExceptionRecordData)
        {
            this.systemExceptionRecordData = systemExceptionRecordData;
            //this.appExceptionRecordData = appExceptionRecordData;
        }

        public void Create(SystemExceptionRecordDto dto)
        {
            this.systemExceptionRecordData.Create(this.AsEntity(dto));

            string smtpHosting = "smtp.mxhichina.com";
            bool enableSSL = true;
            int port = 465;
            string fromUser = "exception@xablackcoffee.com";
            string fromUserPassword = "Mayichuangke312";

            EmailHelper emailHelper = new EmailHelper(smtpHosting, enableSSL, port, fromUser, fromUserPassword);

            var toUser = new string[]
            {
                "wangsijun@xablackcoffee.com",
                "wuzhuanlei@xablackcoffee.com",
                "jiaokang@xablackcoffee.com",
                "bianxiaolong@xablackcoffee.com"
            };

            emailHelper.SendMail($"黑咖啡网络后台异常: {dto.Name} ({dto.Message})", toUser, JsonConvert.SerializeObject(dto, Formatting.Indented), false);
        }
        public void CreateNoneEmail(SystemExceptionRecordDto dto)
        {
            this.systemExceptionRecordData.Create(this.AsEntity(dto));

        }
        public void InsertExceptionNoneEmail(Exception ex, string requestData)
        {
            string source = string.Empty;

            var exceptionEntity = new SystemExceptionRecordEntity
            {
                Name = ex.GetType().Name,
                HelpLink = ex.HelpLink,
                Message = ex.Message,
                StackTrace = ex.StackTrace + requestData,
                HResult = ex.HResult,
                Data = JObject.FromObject(ex.Data).ToString(),
                CreateTime = DateTime.Now,
                InnerException = ex.InnerException?.Message,
                Source = string.IsNullOrWhiteSpace(source) ? ex.Source : source
            };

            this.systemExceptionRecordData.Create(exceptionEntity);
        }
        public void InsertException(Exception ex, string requestData)
        {
            string source = string.Empty;

            var exceptionEntity = new SystemExceptionRecordEntity
            {
                Name = ex.GetType().Name,
                HelpLink = ex.HelpLink,
                Message = ex.Message,
                StackTrace = ex.StackTrace + requestData,
                HResult = ex.HResult,
                Data = JObject.FromObject(ex.Data).ToString(),
                CreateTime = DateTime.Now,
                InnerException = ex.InnerException?.Message,
                Source = string.IsNullOrWhiteSpace(source) ? ex.Source : source
            };

            this.systemExceptionRecordData.Create(exceptionEntity);

            string smtpHosting = "smtp.mxhichina.com";
            bool enableSSL = true;
            int port = 465;
            string fromUser = "exception@xablackcoffee.com";
            string fromUserPassword = "Mayichuangke312";

            EmailHelper emailHelper = new EmailHelper(smtpHosting, enableSSL, port, fromUser, fromUserPassword);

            var toUser = new string[]
             {
                "wangsijun@xablackcoffee.com",
                "wuzhuanlei@xablackcoffee.com",
                "jiaokang@xablackcoffee.com",
                "bianxiaolong@xablackcoffee.com"
             };

            emailHelper.SendMail($"黑咖啡网络后台异常: {exceptionEntity.Name} ({exceptionEntity.Message})", toUser, JsonConvert.SerializeObject(exceptionEntity, Formatting.Indented), false);
        }

        private SystemExceptionRecordEntity AsEntity(SystemExceptionRecordDto entity)
        {
            return new SystemExceptionRecordEntity
            {
                Name = entity.Name,
                HelpLink = entity.HelpLink,
                Source = entity.Source,
                Message = entity.Message,
                StackTrace = entity.StackTrace,
                HResult = entity.HResult,
                Data = entity.Data,
                InnerException = entity.InnerException,
                CreateTime = entity.CreateTime
            };
        }
        
        public PageableList<SystemExceptionRecordDto> GetSystemExceptionRecords(string name, DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
        {
            var data = this.systemExceptionRecordData.GetSystemExceptionRecords(name, start, end, sortName, ascending, pageIndex, pageSize).GetAwaiter().GetResult();

            return new PageableList<SystemExceptionRecordDto>
            {
                Count = data.Item2,
                Items = data.Item1.As<IEnumerable<SystemExceptionRecordDto>>()
            };
        }

        //public void Create(AppExceptionRecordRequestDto appExceptionRecordRequestDto)
        //{
        //    this.appExceptionRecordData.Create(appExceptionRecordRequestDto.As<AppExceptionRecordEntity>());
        //    if (!appExceptionRecordRequestDto.ExceptionName.Contains("SocketTimeout"))
        //    {
        //        string smtpHosting = "smtp.mxhichina.com";
        //        bool enableSSL = true;
        //        int port = 465;
        //        string fromUser = "exception@xablackcoffee.com";
        //        string fromUserPassword = "Mayichuangke312";
        //        EmailHelper emailHelper = new EmailHelper(smtpHosting, enableSSL, port, fromUser, fromUserPassword);
        //        List<string> toUser = new List<string>();
        //        toUser.Add("wangsijun@xablackcoffee.com");
        //        toUser.Add("baimao@xablackcoffee.com");
        //        emailHelper.SendMail("App系统异常通知，请及时处理", toUser.ToArray(), JsonConvert.SerializeObject(appExceptionRecordRequestDto, Formatting.Indented));
        //    }

        //}

        //public PageableList<AppExceptionRecordDto> GetAppExceptionRecords(AppExceptionHandleStatus appExceptionHandleStatus, int pageIndex, int pageSize)
        //{
        //    var list = this.appExceptionRecordData.GetAppExceptionRecords(appExceptionHandleStatus, pageIndex, pageSize).GetAwaiter().GetResult();

        //    return new PageableList<AppExceptionRecordDto>
        //    {
        //        Count = list.Item2,
        //        Items = list.Item1.As<IEnumerable<AppExceptionRecordDto>>()
        //    };
        //}
    }
}
